package chen.nf.util.conterver;

import chen.nf.util.TypeConverter;

import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @Author czl
 * @Date 2024-05-10
 */
public class LocalDateTimeConverter implements TypeConverter {
    @Override
    public boolean supports(Class<?> fieldType) {
        return fieldType.equals(LocalDateTime.class);

    }

    @Override
    public Object apply(Class<?> fieldType, Object value) {
        return switch (value) {
            case LocalDateTime localDateTime -> localDateTime;
            case LocalDate localDate -> localDate.atStartOfDay();
            case Date sqlDate -> sqlDate.toLocalDate().atStartOfDay();
            case Timestamp timestamp -> timestamp.toLocalDateTime();
            case Time time -> time.toLocalTime().atDate(LocalDate.now());
            default -> null;
        };
    }
}
